<?php
namespace app\admin\logic;
use app\admin\validate\WorkRecordData as WorkRecordDataValidate;
use \PhpOffice\PhpSpreadsheet\Spreadsheet;
use \PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class WorkRecordDataLogic extends BaseLogic {

    /**
     * 获取每日数据明细查询条件
     * @param $department
     * @param $username
     * @param $start_time
     * @param $end_time
     * @return array
     */
    public function get_search_wrd_maps($department,$username,$start_time,$end_time){
        $maps = [];

        $department = is_array($department) ? $department : json_decode($department,true);
        if(!in_array('全部',$department)){
            $maps[] = ['department','in',$department];
        }

        if(!empty($username)){
            $u_maps = [];
            $u_maps[] = ['username','like','%' . $username . '%'];
            $openids = model('user')->get_column($u_maps,'openid');
            $maps[] = ['openid','in',$openids];
        }

        if(!empty($start_time) && !empty($end_time)){
            $start_date = date('Y-m-d',strtotime($start_time));
            $end_date = date('Y-m-d',strtotime($end_time));
            $maps[] = ['now_date','between',[$start_date,$end_date]];
        }

        return $maps;
    }

    /**
     * 获取每日数据明细
     * @param $params
     * @return mixed
     */
    public function get_wrd($params){
        $wrd = new \app\common\controller\WorkRecordData();

        $maps = $this->get_search_wrd_maps($params['department'],$params['username'],$params['start_time'],$params['end_time']);
        $limit = ($params['page'] - 1) * $params['rows'];

        $list = model('work_record_data')->get_list($maps,'*','id desc','',$limit,$params['rows']);
        foreach($list as $k => $v){
            $list[$k]['front_money'] = deal_number_div_100($v['front_money']);
            $list[$k]['vip_back_money'] = deal_number_div_100($v['vip_back_money']);
            $list[$k]['check_in_back_money'] = deal_number_div_100($v['check_in_back_money']);
            $list[$k]['agency_back_money'] = deal_number_div_100($v['agency_back_money']);
            $list[$k]['account_back_money'] = deal_number_div_100($v['account_back_money']);
            $list[$k]['trademark_back_money'] = deal_number_div_100($v['trademark_back_money']);
            $list[$k]['science_money'] = deal_number_div_100($v['science_money']);
            $list[$k]['other_money'] = deal_number_div_100($v['other_money']);
            $list[$k]['today_total_money'] = deal_number_div_100($v['today_total_money']);
            $list[$k]['pic'] = !empty($v['pic']) ? json_decode($v['pic'],true) : [];

            //获取目前金额
            $now_money = $wrd->get_month_now_money($v['department'],$v['now_date']);
            $list[$k]['now_money'] = $v['today_total_money'] + $now_money;

            //获取目标金额
            $target_money = $wrd->get_month_target_money($v['department'],$v['now_date']);
            $list[$k]['target_money'] = $target_money;

            //查找用户
            $u_maps = [];
            $u_maps[] = ['openid','=',$v['openid']];
            $u_detail = model('user')->get_detail($u_maps,'id,username');
            $list[$k]['username'] = $u_detail['username'];
        }

        $total_num = model('work_record_data')->get_count($maps,'id');

        $data['list'] = $list;
        $data['total_num'] = $total_num;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 检查修改每日数据明细规则
     * @param $params
     * @return mixed
     */
    public function check_save_wrd_rule($params){
        $validate = new WorkRecordDataValidate();
        $r = $validate->scene('edit')->check($params);
        if(!$r){
            return $this->get_response(0,$validate->getError());
        }

        $maps = [];
        $maps[] = ['id','=',$params['wrdid']];
        $wrd_detail =  model('work_record_data')->get_detail($maps,'id');
        if(empty($wrd_detail)){
            return $this->get_response(0,'该记录不存在');
        }

        $data['wrd_detail'] = $wrd_detail;
        return $this->get_response(1,'OK',$data);
    }

    /**
     * 生成每日数据明细excel
     * @param $ExcelName
     * @param $data
     * @return bool
     */
    public function gen_wrd_excel($ExcelName,$data){
        if(count($data[0]) > 26){
            return $this->get_response(0,'导出excel最大列数为26，请缩减数据列数');
        }
        $spreadsheet = new Spreadsheet();
        $sheet = $spreadsheet->getActiveSheet();
        for($row = 1;$row <= count($data);$row++){
            $index = 0;
            foreach($data[$row-1] as $k => $v) {
                $col = chr($index + 65);
                $cell = ($col . $row);
                $sheet->setCellValue($cell, $v);
                $index++;
            }
        }

        header('Content-Type: application/vnd.ms-excel;charset=utf8-general-ci');
        header('Content-Disposition: attachment;filename='.$ExcelName);
        header('Cache-Control: max-age=0');

        $writer = new Xlsx($spreadsheet);
        $writer->save('php://output');
        //$writer->save($ExcelName);
        return true;
    }
}